﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

public partial class Order_OrderList : System.Web.UI.Page
{
    private bool IsUseColorSize
    {
        get
        {
            return new BLL.SYSPARAM().GetProductUpDownColorSizeConfig();
        }
    }

    private bool SelectProductConfig
    {
        get
        {
            return new BLL.SYSPARAM().GetSelectProductConfig();
        }
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check.CheckPermission("400101");
        Session["FCode"] = "400101";
        if (!IsPostBack)
        {
            this.Panel1.Visible = false;
            TextBox2.Attributes.Add("onclick", "SearchVIP('../');");
            //((Button)BUTTONDJLIST1.Controls[4]).Enabled = false;
            //((Button)BUTTONDJLIST1.Controls[6]).Enabled = false;
            ORDER_OFCODE();
            ORDER_DTCODE();
            BindDropOrderSuggestion();
            //绑定支付方式
            List<Model.PAYTYPE> paytype = new List<Model.PAYTYPE>();
            paytype = new BLL.PAYTYPE().GetAllList();
            ddlPaylist.DataSource = paytype;
            ddlPaylist.DataTextField = "PT_NAME";
            ddlPaylist.DataValueField = "PT_CODE";
            ddlPaylist.DataBind();
            ListItem li = new ListItem();
            li.Text = "全部";
            li.Value = "-1";
            ddlPaylist.Items.Insert(0, li);
            Bind(false);

            drpColor.Items.Insert(0, new ListItem("", "-1"));
            drpSize.Items.Insert(0, new ListItem("", "-1"));

        }
    }
    private void ORDER_OFCODE()
    {
        List<Model.ORDERFROM> properties = new BLL.ORDERFROM().GetAllList();
        DropDownList1.DataSource = properties;
        DropDownList1.DataTextField = "OF_NAME";
        DropDownList1.DataValueField = "OF_CODE";
        DropDownList1.DataBind();
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        DropDownList1.Items.Insert(0, li);
    }
    private void ORDER_DTCODE()
    {
        List<Model.DEALTYPE> properties = new BLL.DEALTYPE().GetAllList();
        DropDownList2.DataSource = properties;
        DropDownList2.DataTextField = "DT_NAME";
        DropDownList2.DataValueField = "DT_CODE";
        DropDownList2.DataBind();
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        DropDownList2.Items.Insert(0, li);
    }
    private void BindDropOrderSuggestion()
    {
        dropOrderSuggestion.DataSource = new BLL.STATETYPE().GetAllList();
        dropOrderSuggestion.DataTextField = "ST_NAME";
        dropOrderSuggestion.DataValueField = "ST_CODE";
        dropOrderSuggestion.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        dropOrderSuggestion.Items.Insert(0, li);
    }

    private void Bind(bool isExport)
    {

        //根据支付方式查出相关的订单编号



        string sqlstr = " ";
        sqlstr += TextBox1.Text.Trim() == "" ? "" : " and order_id like '%" + TextBox1.Text.Trim() + "%' ";
        sqlstr += TextBox2.Text.Trim() == "" ? "" : " and order_customercode like '%" + TextBox2.Text.Trim() + "%' ";
        sqlstr += txtNickName.Text.Trim() == "" ? "" : " and V_Customer.byzd5 like '%" + txtNickName.Text.Trim() + "%' ";
        if (TextBox3.Text.Trim().Length > 0)
        {
            sqlstr += " and order_createdate>='" + TextBox3.Text.Trim() + "'";
        }
        if (TextBox4.Text.Trim().Length > 0)
        {
            sqlstr += "and order_createdate<='" + DateTime.Parse(TextBox4.Text.Trim()).AddDays(1) + "'";
        }
        //sqlstr += TextBox3.Text.Trim() == "" ? " and (order_createdate>'1900-01-01' " : " and (order_createdate>='" + TextBox3.Text.Trim() + "' ";
        //sqlstr += TextBox4.Text.Trim() == "" ? " and order_createdate<'2099-01-01') " : " and order_createdate<='" + DateTime.Parse(TextBox4.Text.Trim()).AddDays(1).ToShortDateString() + "') ";
        sqlstr += DropDownList1.SelectedValue.Trim() == "-1" ? "" : " and ORDER_OFCODE='" + DropDownList1.SelectedValue + "' ";
        sqlstr += DropDownList2.SelectedValue.Trim() == "-1" ? "" : " and PD_DTCODE='" + DropDownList2.SelectedValue + "' ";
        sqlstr += CheckBox1.SelectedValue == "-1" ? "" : " and ORDER_CHECK=" + Int32.Parse(CheckBox1.SelectedValue) + " ";
        sqlstr += CheckBox2.SelectedValue == "-1" ? "" : " and ORDER_ACHECK=" + Int32.Parse(CheckBox2.SelectedValue) + " ";
        sqlstr += CheckBox3.SelectedValue == "-1" ? "" : " and ORDER_DELETE=" + Int32.Parse(CheckBox3.SelectedValue) + " ";
        sqlstr += CheckBox4.SelectedValue == "-1" ? "" : " and ORDER_SEND=" + Int32.Parse(CheckBox4.SelectedValue) + " ";
        sqlstr += CheckBox5.SelectedValue == "-1" ? "" : " and ORDER_LOCK=" + Int32.Parse(CheckBox5.SelectedValue) + " ";
        sqlstr += dropClear.SelectedValue == "-1" ? "" : " and ORDER_CLEAR=" + Int32.Parse(dropClear.SelectedValue) + " ";
        sqlstr += ddlPaylist.SelectedValue.Trim() == "-1" ? "" : " and PD_PAYTYPE='" + ddlPaylist.SelectedValue + "' ";
        sqlstr += DropDownList3.SelectedValue.Trim() == "-1" ? "" : " and ORDER_SECDEAL=" + DropDownList3.SelectedValue + " ";
        sqlstr += DropDownList4.SelectedValue.Trim() == "-1" ? "" : " and ORDER_APAY=" + DropDownList4.SelectedValue + " ";
        sqlstr += TextBox5.Text.Trim() == "" ? "" : " and ORDER_GETER like '%" + TextBox5.Text.Trim() + "%' ";
        sqlstr += DropDownList5.SelectedValue == "-1" ? "" : " and ORDER_IsPromotion=" + DropDownList5.SelectedValue + " ";
        sqlstr += dropOrderSuggestion.SelectedValue == "-1" ? "" : " and ORDER_STATETYPECODE='" + dropOrderSuggestion.SelectedValue + "' ";
        sqlstr += txtDealNo.Text.Trim() == "" ? "" : " and PD_DEALID like '%" + txtDealNo.Text.Trim() + "%' ";
        if (this.Panel1.Visible == true)
        {
            sqlstr += txtgddh.Text.Trim() == "" ? "" : " and ORDER_GETERPHONE1 LIKE '%" + txtgddh.Text.Trim() + "%' ";
            sqlstr += txtsj.Text.Trim() == "" ? "" : " and ORDER_GETERPHONE2 LIKE '%" + txtsj.Text.Trim() + "%' ";
            sqlstr += txtaddress.Text.Trim() == "" ? "" : " and ORDER_GETADDRESS LIKE '%" + txtaddress.Text.Trim() + "%' ";
            sqlstr += txtspdm.Text.Trim() == "" ? "" : " and OD_CMDCODE LIKE '%" + txtspdm.Text.Trim() + "%' ";
            sqlstr += drpColor.SelectedValue == "-1" ? "" : " and OD_COLORCODE = '" + drpColor.SelectedValue + "' ";
            sqlstr += drpSize.SelectedValue == "-1" ? "" : " and OD_SIZECODE= '" + drpSize.SelectedValue + "' ";
        }

        int pageCount, rowCount;
        //DataSet properties = new BLL.ORDERDOC().GetList2("orderdoc left join PAYDETAIL on ORDER_ID=PD_ID  left join ORDERDETAIL on order_id=od_id ", "orderdoc.*", false, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "ORDER_CREATEDATE", true, sqlstr, out pageCount, out rowCount);
        DataSet properties = new BLL.ORDERDOC().P_GetOrderList_OrderBy(AspNetPager1.StartRecordIndex, AspNetPager1.PageSize, sqlstr, "order_createdate");
        rowCount = Int32.Parse(properties.Tables[1].Rows[0][0].ToString());
        if (isExport)  //如果是导出
        {
            properties = new BLL.ORDERDOC().P_GetOrderList_OrderBy(AspNetPager1.StartRecordIndex, rowCount, sqlstr, "order_createdate");
            DataTable2Excel(properties.Tables[0]);
        }
        pageCount = rowCount / AspNetPager1.PageSize;
        ORDERDOCListControl1.Bind(properties.Tables[0]);
        AspNetPager1.RecordCount = rowCount;
    }

    /// <summary>
    /// DataTable导出Excel
    /// </summary>
    /// <param name="dtData"></param>
    public static void DataTable2Excel(System.Data.DataTable dtData)
    {
        System.Web.UI.WebControls.DataGrid dgExport = null;
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        System.IO.StringWriter strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";

            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            dgExport = new System.Web.UI.WebControls.DataGrid();

            dtData.Columns.Add("锁定");
            dtData.Columns.Add("客审");
            dtData.Columns.Add("财审");
            dtData.Columns.Add("发货");
            dtData.Columns.Add("结算");
            dtData.Columns.Add("作废");
            dtData.Columns.Add("担保交易");
            dtData.Columns.Add("货到付款");
            dtData.Columns.Add("促销");

            dtData.Columns["id"].ColumnName = "序号";
            dtData.Columns["O_OrderId"].ColumnName = "单据编号";
            dtData.Columns["ORDER_CUSTOMERCODE"].ColumnName = "用户帐号 ";
            //dtData.Columns["发货/入库"].DataType = Type.GetType("System.bool");  
            //dtData.Columns["Clear"].ColumnName = "用户真实姓名 ";
            //dtData.Columns["财务审核"].DataType = Type.GetType("System.bool"); 
            //dtData.Columns["Acheck"].ColumnName = "结算";
            //dtData.Columns["结算"].DataType = Type.GetType("System.bool"); 

            dtData.Columns["ss"].ColumnName = "单据生成时间";
            //dtData.Columns["CMDCODE"].ColumnName = "商品代码";
            //dtData.Columns["SPMC"].ColumnName = "商品ERP名称";
            //dtData.Columns["CA_NAME"].ColumnName = "商品电子商务名称";
            dtData.Columns["ORDER_QTY"].ColumnName = "数量";
            //dtData.Columns["Sprice"].ColumnName = "标准单价";
            //dtData.Columns["Disc"].ColumnName = "折扣";
            //dtData.Columns["Aprice"].ColumnName = "实际单价";
            //dtData.Columns["Samt"].ColumnName = "标准金额";
            dtData.Columns["ORDER_TAMT"].ColumnName = "总额";
            dtData.Columns["ORDER_TRANCOST"].ColumnName = "运费";
            dtData.Columns["ORDER_GETER"].ColumnName = "会员名称";

            dtData.Columns["ORDER_GETERPHONE1"].ColumnName = "电话";
            dtData.Columns["ORDER_GETERPHONE2"].ColumnName = "手机";
            dtData.Columns["ORDER_GETADDRESS"].ColumnName = "地址";
            dtData.Columns["ORDER_GETPOSTCODE"].ColumnName = "邮编";
            dtData.Columns["ORDER_CREATER"].ColumnName = "创建人";

            for (int i = 0; i < dtData.Rows.Count; i++)
            {
                dtData.Rows[i]["客审"] = dtData.Rows[i]["ORDER_CHECK"].ToString() == "1" ? "已客审" : "未客审";
                dtData.Rows[i]["财审"] = dtData.Rows[i]["ORDER_ACHECK"].ToString() == "1" ? "已审核" : "未审核";
                dtData.Rows[i]["锁定"] = dtData.Rows[i]["ORDER_LOCK"].ToString() == "1" ? "已锁定" : "未锁定";
                dtData.Rows[i]["作废"] = dtData.Rows[i]["ORDER_DELETE"].ToString() == "1" ? "已作废" : "未作废";
                dtData.Rows[i]["结算"] = dtData.Rows[i]["ORDER_CLEAR"].ToString() == "1" ? "已结算" : "未结算";
                dtData.Rows[i]["发货"] = dtData.Rows[i]["ORDER_SEND"].ToString() == "1" ? "已发货" : "未发货";
                dtData.Rows[i]["担保交易"] = dtData.Rows[i]["ORDER_SECDEAL"].ToString() == "1" ? "是" : "否";
                dtData.Rows[i]["货到付款"] = dtData.Rows[i]["ORDER_APAY"].ToString() == "1" ? "是" : "否";
                dtData.Rows[i]["促销"] = dtData.Rows[i]["ORDER_IsPromotion"].ToString() == "1" ? "已促销" : "未促销";
            }
            ////移除列 
            dtData.Columns.Remove(dtData.Columns["ORDER_CHECK"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_ACHECK"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_LOCK"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_DELETE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_CLEAR"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_SEND"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_SECDEAL"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_APAY"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_IsPromotion"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_AID"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_DTCODE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_ID"]);

            dtData.Columns.Remove(dtData.Columns["ORDER_DISC"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_AMT"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_OFFERAMT"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_AAMT"]);

            dtData.Columns.Remove(dtData.Columns["ORDER_FIELD1"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_STATETYPECODE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_NOTE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_FIELD2"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_ITGBUY"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_ACHECKDATE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_CREATEDATE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_COSTNATURE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_TRANERCODE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_TRANBILLCODE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_OFCODE"]);
            dtData.Columns.Remove(dtData.Columns["CouponMoney"]);
            dtData.Columns.Remove(dtData.Columns["CouponNo"]);
            dtData.Columns.Remove(dtData.Columns["Coupon"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_JFSendTime"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_SendAmt"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_AmtSendTime"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_SendJF"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_PAYAMT"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_HAVEPAYED"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_TPRINTDATE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_TPRINTCOUNT"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_LOCKER"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_PROVINCE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_CITY"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_LOCKDATE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_AREA"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_PRINT"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_UPLOCKER"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_UPLOCKDATE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_TPRINT"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_TPRINTER"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_WEIGHT"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_CLEARAMT"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_SENDDATE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_PRINTDATE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_PRINTER"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_PRINTCOUNT"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_SENDER"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_OFFERID"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_NINV"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_INVTITLE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_OFFERTYPE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_DELETER"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_DELETEDATE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_CHECKDATE"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_ACHECKER"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_CHECKER"]);
            dtData.Columns.Remove(dtData.Columns["ORDER_ITG"]);

            for (int _t = 3; _t <= 30; _t++)
            {
                dtData.Columns.Remove(dtData.Columns["BYZD" + _t + ""]);
            }

            dgExport.DataSource = dtData.DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();

            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        Bind(false);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind(false);
    }
    public void Add()
    {
        if (Check.CheckPermission("400101", "00001"))
        {
            Response.Redirect("NewOrder.aspx");
        }
        else
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
    }
    public void Update()
    {
        if (Check.CheckPermission("400101", "00002"))
        {
            List<string> s = ORDERDOCListControl1.Selected;
            if (s.Count > 0)
            {
                Response.Redirect("OrderDetail.aspx?code=" + s[0]);
            }
            else
            {
                Share.Alert(this.Page, "请选择一条需要编辑的记录"); return;
            }
        }
        else
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
    }
    /// <summary>
    /// by winner add 2008-04-20  打印
    /// </summary>
    public void Print()
    {
        //if (Check.CheckPermission("400101", "00002"))
        //{
        List<string> s = ORDERDOCListControl1.Selected;
        if (s.Count > 0)
        {
            string openurlstr = "";
            string sprinttype = "0";//默认打印时不显示界面
            if (ConfigurationManager.AppSettings["PrintType"] != null && ConfigurationManager.AppSettings["PrintType"] != "") sprinttype = ConfigurationManager.AppSettings["PrintType"];
            //By winner add打印
            for (int i = 0; i < s.Count; i++)
            {
                openurlstr += "window.open('../BillReport.aspx?DJBH=" + s[i] + "&ReportModule=DDGL/SDDHD&ReportName=SDDHD&PrintType=" + sprinttype + "');";
            }
            Page.RegisterStartupScript("StartUp", "<script language=javascript>" + openurlstr + "</script>");

            //Response.Redirect("ReturnDocEdit.aspx?code=" + s[0]);
        }
        else
        {
            Share.Alert(this.Page, "请选择要打印的订单"); return;
        }
        //}
        //else
        //{
        //    Share.Alert(this.Page, "无权操作"); return;
        //}
    }

    /// <summary>
    /// by winner add 2008-04-20  导出
    /// </summary>
    public void Out()
    {
        Bind(true);
        //Share.Alert(this.Page, "功能完善中..."); return;
    }
    protected void btngjcx_Click(object sender, EventArgs e)
    {


    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (this.Panel1.Visible == false)
        {
            this.Panel1.Visible = true;
            Button3.Text = "<<";

        }
        else
        {
            this.Panel1.Visible = false;
            Button3.Text = ">>";

        }


    }

    private bool CheckJS(List<string> list, out string alert)
    {
        alert = "";

        string s1 = "";// "系统无法获取订单的信息";

        string s2 = ""; //"订单已经结算，不允许操作";

        string s3 = ""; //"非货到付款的订单不能参与结算";

        string s4 = "";// "订单尚未发货，不能参与结算"

        string s5 = "";//系统内无会员信息

        for (int i = 0; i < list.Count; i++)
        {
            Model.ORDERDOC order = new BLL.ORDERDOC().GetModel(list[i]);
            if (order == null)
            {
                s1 += list[i] + ",";
            }
            if (order.ORDER_CLEAR == 1)
            {
                s2 += list[i] + ",";
            }
            if (order.ORDER_APAY == 0)
            {
                s3 += list[i] + ",";
            }

            if (order.ORDER_SEND == 0)
            {
                s4 += list[i] + ",";
            }

            Model.V_VIPSET userInfo = new BLL.V_VIPSET().GetModel(order.ORDER_CUSTOMERCODE);
            if (userInfo == null)
            {
                s5 += order.ORDER_CUSTOMERCODE + ",";
            }
        }
        if (s1.Length > 0)
        {
            alert = "系统无法获取订单:" + s1.Substring(0, s1.Length - 1) + "的信息！";
            return false;
        }
        if (s2.Length > 0)
        {
            alert = "订单:" + s2.Substring(0, s2.Length - 1) + "已经结算，不允许操作！";
            return false;
        }
        if (s3.Length > 0)
        {
            alert = "非货到付款的订单:" + s3.Substring(0, s3.Length - 1) + ",不能参与结算！";
            return false;
        }
        if (s4.Length > 0)
        {
            alert = "订单:" + s4.Substring(0, s4.Length - 1) + "尚未发货，不能参与结算！";
            return false;
        }
        if (s5.Length > 0)
        {
            alert = "系统内无会员:" + s5.Substring(0, s5.Length - 1) + "的信息！";
            return false;
        }
        return true;
    }

    private void JieSuan(List<string> list, string payType, out StringBuilder dbSql, out StringBuilder erpSql)
    {

        dbSql = new StringBuilder();
        erpSql = new StringBuilder();

        string operatorId = Share.UserValue(0);
        for (int i = 0; i < list.Count; i++)
        {   //插入支付明细
            Model.ORDERDOC order = new BLL.ORDERDOC().GetModel(list[i]);

            dbSql.Append("INSERT INTO [PAYDETAIL]([PD_ID],[PD_DETAILID],[PD_PAYTYPE],[PD_PAYAMT],[PD_PAYDATE],");
            dbSql.Append("[PD_DEALID],[PD_DEALDATE],[PD_ACCOUNT],[PD_GET],[PD_GETAMT],[PD_GETDATE],[PD_DTCODE]) VALUES('");
            dbSql.Append(order.ORDER_ID);
            dbSql.Append("','");
            dbSql.Append(1);
            dbSql.Append("','");
            dbSql.Append(payType);
            dbSql.Append("',");
            dbSql.Append(order.ORDER_AAMT);
            dbSql.Append(",'");
            dbSql.Append(DateTime.Now);
            dbSql.Append("','");
            dbSql.Append("''");
            dbSql.Append("','");
            dbSql.Append(DateTime.Now);
            dbSql.Append("','");
            dbSql.Append("''");
            dbSql.Append("',1,");
            dbSql.Append(order.ORDER_AAMT);
            dbSql.Append(",'");
            dbSql.Append(DateTime.Now);
            dbSql.Append("','");
            dbSql.Append("001");
            dbSql.Append("')|");

            //赠送礼券
            StringBuilder dbSB = new StringBuilder();
            StringBuilder erpSB = new StringBuilder();

            BLL.Promotion.BuildInitCouponSqlString(order.ORDER_AAMT, order.ORDER_ID, order.ORDER_CUSTOMERCODE, out dbSB, out erpSB);
            dbSql.Append(dbSB.ToString());
            erpSql.Append(erpSB.ToString());

            //修改订单结算状态
            dbSql.Append("update orderdoc set ORDER_CLEAR=1,ORDER_CLEARAMT=");
            dbSql.Append(order.ORDER_AAMT);
            dbSql.Append(",ORDER_PAYAMT=");
            dbSql.Append(order.ORDER_AAMT);
            dbSql.Append(",BYZD9='");
            dbSql.Append(DateTime.Now);
            dbSql.Append("' where ORDER_ID='");
            dbSql.Append(order.ORDER_ID);
            dbSql.Append("'|");


            //插入会员信息
            Model.V_VIPSET userInfo = new BLL.V_VIPSET().GetModel(order.ORDER_CUSTOMERCODE);

            Model.V_VIP_ACCOUNT userAccount = new BLL.V_VIP_ACCOUNT().GetModel(userInfo.DM);

            if (userAccount == null)
            {
                dbSql.Append("insert into v_vip_account(DM,DQJE_1) values('");
                dbSql.Append(userInfo.DM);
                dbSql.Append("',0)|");

                erpSql.Append("insert into v_vip_account(DM,DQJE_1) values('");
                erpSql.Append(userInfo.DM);
                erpSql.Append("',0)|");
            }

        }
    }

    protected void btnJS_Click(object sender, EventArgs e)
    {

        if (Check.CheckPermission("400101", "00015"))
        {
            string alert = "";
            List<string> s = ORDERDOCListControl1.Selected;
            if (s.Count > 0)
            {
                try
                {
                    if (!CheckJS(s, out alert))
                    {//批量检查
                        throw new Exception(alert);

                    }

                    StringBuilder strSqlToUpdateBStyleDB = new StringBuilder();
                    StringBuilder strSqlToUpdateErpDB = new StringBuilder();

                    string payType = hdPayType.Value;
                    bool result = false;

                    JieSuan(s, payType, out strSqlToUpdateBStyleDB, out strSqlToUpdateErpDB);

                    if (strSqlToUpdateErpDB.ToString() == "")
                    {
                        string stringSql = strSqlToUpdateBStyleDB.ToString();
                        char[] charToTrim = new char[] { '|' };
                        string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                        result = new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray);
                    }
                    else
                    {
                        string stringSqlToUpdateBStyleDB = strSqlToUpdateBStyleDB.ToString();
                        string stringSqlToUpdateErpDB = strSqlToUpdateErpDB.ToString();

                        char[] charToTrim = new char[] { '|' };
                        string[] strArray1 = stringSqlToUpdateBStyleDB.TrimEnd(charToTrim).Split(charToTrim);
                        string[] strArray2 = stringSqlToUpdateErpDB.TrimEnd(charToTrim).Split(charToTrim);

                        LX_Manage.ERP.Service webService = new LX_Manage.ERP.Service();
                        result = webService.UpdateBalanceAndUsableDeliverAmountOfOneRecord(ConfigurationManager.AppSettings["WebServiceKey"].ToString(), strArray1, strArray2);
                    }
                    if (result)
                    {
                        Response.Redirect("OrderList.aspx", true);
                    }
                    else
                    {
                        throw new Exception("结算失败");
                    }

                }
                catch (Exception ex)
                {
                    Share.Alert(this.Page, ex.Message); return;
                }

            }
            else
            {
                Share.Alert(this.Page, "请选择一条需要编辑的记录"); return;
            }
        }
        else
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
    }
    private void BindColor(string productCode)
    {

        DataSet dstProductColor = new BLL.SHANGPIN().GetSPGG1(productCode);
        drpColor.DataSource = dstProductColor;
        drpColor.DataTextField = "ggmc";
        drpColor.DataValueField = "ggdm";
        drpColor.DataBind();
        drpColor.Items.Insert(0, new ListItem("", "-1"));
    }
    private void BindSize(string productCode)
    {
        DataSet dstProductSize = new BLL.SHANGPIN().GetSPGG2(productCode);
        drpSize.DataSource = dstProductSize;
        drpSize.DataTextField = "ggmc";
        drpSize.DataValueField = "ggdm";
        drpSize.DataBind();
        drpSize.Items.Insert(0, new ListItem("", "-1"));

    }
    protected void txtspdm_TextChanged(object sender, EventArgs e)
    {
        if (txtspdm.Text.Trim().Length > 0)
        {
            BindColor(txtspdm.Text.Trim());
            BindSize(txtspdm.Text.Trim());

        }
        else
        {
            drpColor.Items.Clear();
            drpSize.Items.Clear();

            drpColor.Items.Insert(0, new ListItem("", "-1"));
            drpSize.Items.Insert(0, new ListItem("", "-1"));
        }
    }
}
